package com.example.juc.sort;

import org.junit.Test;

import java.util.Arrays;

public class Client {

    @Test
    public void test() {
        int numberCount = 10;
        CallableSort callableSort = new CallableSort(numberCount);

        int[] generateNumbers = callableSort.getGenerateNumbers();
        System.out.println("待排序数据 ---> " + Arrays.toString(generateNumbers));

        System.out.println("-----------------------------");
        singleThreadSort(callableSort);

        System.out.println("-----------------------------");
        multiThreadSort(callableSort, 10);
    }

    private void multiThreadSort(CallableSort callableSort, int groupSize) {
        long start;
        long end;
        start = System.nanoTime();

        // 多线程排序
        int[] ans = callableSort.multiThreadSort(groupSize);

        end = System.nanoTime();
        // 打印多线程排序结果
        System.out.println("多线程排序后返回数组 --->" + Arrays.toString(ans));
        // 打印多线程耗时
        System.out.println("多线程耗时：" + (end - start) * 1.0 / Math.pow(10, 6) + "ms");
    }

    private void singleThreadSort(CallableSort callableSort) {
        // 单线程排序
        long start = System.nanoTime();

        // 普通单线程排序
        int[] ans = callableSort.singleThreadSort();

        long end = System.nanoTime();
        // 打印单线程排序结果
        System.out.println("单线程排序后原数组 --->" + Arrays.toString(ans));
        // 输出单线程耗时
        System.out.println("单线程耗时：" + (end - start) * 1.0 / Math.pow(10, 6) + "ms");
    }
}
